package cap5;
//BitmapTest program
import java.awt.*;
import java.util.*;
import java.net.*;
import javax.swing.*;
public class BitmapTest extends JFrame implements Runnable {
Image image;
Thread gameloop;
Random rand = new Random();
public static void main(String[] args) {
new BitmapTest();
}
public BitmapTest() {
super("Opaque Bitmap Test");
setSize(1000,980);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Toolkit tk = Toolkit.getDefaultToolkit();
image = tk.getImage(getURL("shipTranparent.png"));
gameloop = new Thread(this);
gameloop.start();
}
private URL getURL(String filename) {
URL url = null;
try {
url = this.getClass().getResource(filename);
}
catch (Exception e) { }
return url;
}
public void run() {
Thread t = Thread.currentThread();
while (t == gameloop) {
try {
Thread.sleep(20);
}
catch (InterruptedException e) {
e.printStackTrace();
}
repaint();
}
}
public void update(Graphics g) {
paint(g);
}
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
int width = 1000 - image.getWidth(this);
int height = 980 - image.getHeight(this);
g2d.setColor(Color.BLUE);
g2d.drawImage(image, rand.nextInt(width), rand.nextInt(height), this);
}
}
